package com.example.demo.web.rest.controller;

import com.ffcs.oss.annotation.PermissionAuth;
import com.ffcs.oss.param.out.ServiceResp;
import com.ffcs.oss.param.vm.QueryPageVm;
import com.example.demo.service.ResourceService;
import com.example.demo.web.rest.evt.*;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@ApiModel(description ="资源接口")
@RestController
@RequestMapping(value = "/api/resource")
public class ResourceController {

    protected Logger logger= LoggerFactory.getLogger(getClass());
    @Autowired
    private ResourceService resourceService;

    @ApiOperation("查询资源信息")
    @PostMapping("/queryResource")
    public ServiceResp<QueryPageVm> queryResource(@RequestBody QueryResourceEvt evt){
        return ServiceResp.getInstance(QueryPageVm.class).success(resourceService.queryResource(evt));
    }

    @ApiOperation("新增/修改资源信息")
    @PermissionAuth(code = "OSS-SETTING-RESOURCE-ADD,OSS-SETTING-RESOURCE-EDIT")
    @PostMapping("/saveResource")
    public ServiceResp saveResource(@RequestBody SaveResourceEvt evt){
        try{
            resourceService.saveResource(evt);
            return ServiceResp.getInstance().success("保存成功");
        }catch (Exception e){
            return ServiceResp.getInstance().error(e.getMessage());
        }
    }

    @ApiOperation("删除资源信息")
    @PermissionAuth(code = "OSS-SETTING-RESOURCE-DEL")
    @PostMapping("/deleteResource")
    public ServiceResp deleteResource(@RequestBody IdEvt evt){
        try{
            resourceService.deleteResource(evt.getId());
            return ServiceResp.getInstance().success("删除成功");
        }catch (Exception e){
            return ServiceResp.getInstance().error(e.getMessage());
        }
    }
}
